/**
 * 离线视频录制器。
 */
import QtQuick 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4
import QtMultimedia 5.8

import "../widgets" as Widgets
import "../../js/dialog/VideoRecorderDialog.js" as Js

ApplicationWindow {
    id: videoRecorderView

    title: "离线视频"

    width: 640
    height: 360
    flags: Qt.Window | Qt.WindowStaysOnTopHint

    readonly property var showIt: Js.showIt
    property string name

    // 私有属性
    Item{
        id: props

        property string videoFilePath: "file:///Users/sand/Desktop/" + name + ".mp4"
    }

    // 摄像头
    Camera {
        id: camera

        // 闪光模式
        flash.mode: Camera.FlashRedEyeReduction

        // 白平衡模式
        imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash

        // 曝光值
        exposure {
            exposureCompensation: 0.0
            exposureMode: Camera.ExposureLandscape
        }

        // 捕捉到一幅画面后，立即显示到预览控件上
        imageCapture {
            onImageCaptured: {
                photoPreview.source = preview  // Show the preview in an Image
            }
        }

        captureMode: Camera.CaptureVideo

        // 视频录像
        videoRecorder{
            audioBitRate: 96000
            audioCodec: "aac"
            audioEncodingMode: CameraRecorder.ConstantBitRateEncoding
            audioSampleRate: 8000

            resolution.width: 640
            resolution.height: 360
            frameRate: 30.00
            //videoCodec: "h264"
            videoBitRate: CameraRecorder.ConstantQualityEncoding

            mediaContainer: "mp4"
            outputLocation: props.videoFilePath
        }
    }

    // 预览画面
    Image {
        id: photoPreview
    }

    // 播放器
    MediaPlayer {
        id: player

        source: props.videoFilePath
        autoPlay: false

        onStopped: {
            videoOutput.source = camera;
            camera.start();
        }
    }

    // 视频输出画面
    VideoOutput {
        id: videoOutput

        anchors.fill: parent

        source: camera
        focus : visible // to receive focus and capture key events when visible
    }

    // 控件布局层
    ColumnLayout {
        id: controlLayout

        anchors.fill: parent

        z: 100
        visible: true

        RowLayout {
            id: user

            anchors.left: parent.left
            anchors.top: parent.top
            anchors.leftMargin: 20
            anchors.topMargin: 50

            Widgets.RoundImage{
                id: avatar

                width: 64
                height: 64
                imageSource: "qrc:/images/avatar_default.png"

                Layout.rowSpan: 2
            }

            ColumnLayout {
                anchors.left: avatar.right
                anchors.leftMargin: 10

                Text {
                    text: name
                }
            }
        }

        RowLayout {
            anchors.horizontalCenter: parent.horizontalCenter

            Button {
                text: camera.videoRecorder.recorderState !== CameraRecorder.RecordingState ? "Start" : "Stop"

                onClicked: {
                    console.debug("player.source", player.source);
                    if(camera.videoRecorder.recorderState === CameraRecorder.StoppedState){
                        camera.videoRecorder.record();
                    } else {
                        camera.videoRecorder.stop();
                    }
                }
            }

            Button {
                id: stopRecord
                text: player.playbackState != MediaPlayer.PlayingState ? "Play" : "Stop"
                enabled: camera.videoRecorder.recorderState === CameraRecorder.StoppedState

                onClicked: {
                    if(player.playbackState == MediaPlayer.PlayingState){
                        player.stop();

                        videoOutput.source = camera;
                        camera.start();
                    } else {
                        camera.stop();
                        videoOutput.source = player;

                        player.play();
                    }
                }
            }
        }
    }

    onClosing: {
        camera.stop();
    }

    // 鼠标移动的时候，显示控件，并开启定时器，时间到之后隐藏
    MouseArea {
        anchors.fill: parent

        hoverEnabled: true
        onPositionChanged: {
            controlLayout.visible = true;
            controlLayoutTimer.start();
        }
    }

    Timer {
        id: controlLayoutTimer

        interval: 1500
        repeat: false
        onTriggered: {
            controlLayout.visible = false;
            controlLayoutTimer.stop();
        }
    }
}
